package com.gm.controller.right;

import com.gm.common.Pager;
import com.gm.model.Base.Const;
import com.gm.model.po.Right;
import com.gm.model.vo.ResMsg;
import com.gm.service.user.RightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * Created by Administrator on 2017/6/13.
 */

@Controller
@RequestMapping("right")
public class RightController {
    @Autowired
   private  RightService rightService;

    @RequestMapping("rightList.web")
    public ModelAndView roleList(Pager pager){
        ModelAndView view= new ModelAndView();
        List<Right> rightList = rightService.getALlRightList();
        view.addObject("rightList", rightList);
        view.setViewName("/right/right.jsp");
        return view;
    }

    @RequestMapping("gotoAddRight.web")
    public ModelAndView gotoAddRight(Integer parentNode){
        ModelAndView view= new ModelAndView();
        view.addObject("parentNode", parentNode);
        view.setViewName("/right/addRight.jsp");
        return view;
    }


    @RequestMapping(value = "addRight.do", method = RequestMethod.POST)
    @ResponseBody
    public ResMsg addRight(Right right, RedirectAttributes attr){
        ResMsg res = new ResMsg();
        int ret =  rightService.addRight(right);
        if(ret == 1){
            res.setStatus(Const.RES_OK);
        }else{
            res.setError("新增权限失败!");
        }
        return res;
    }


    @RequestMapping("gotoUpdateRight.web")
    public ModelAndView gotoUpdateRole(Integer rightId){
        ModelAndView view= new ModelAndView();
        Right right = rightService.selectByPrimaryKey(rightId);
        view.addObject("right", right);
        view.setViewName("/right/updateRight.jsp");
        return view;
    }

    @RequestMapping("updateRight.web")
    public ModelAndView updateRight(Right right, RedirectAttributes attr){
        ModelAndView view= new ModelAndView();
        int ret =  rightService.updateRight(right);
        attr.addFlashAttribute("msg", "修改成功");
        view.setViewName("redirect:../right/rightList.web");
        return view;
    }
    @RequestMapping("deleteRight.web")
    public ModelAndView deleteRight(Integer rightId, RedirectAttributes attr){
        ModelAndView view= new ModelAndView();
        int ret =  rightService.deleteRight(rightId);
        attr.addFlashAttribute("msg", "删除成功");
        view.setViewName("redirect:../right/rightList.web");
        return view;
    }

}
